//
//  NoticeDaol.m
//  Contacts
//
//  Created by 武 娇 on 14-8-2.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "NoticeDao.h"
#import "DateMethod.h"

@implementation NoticeDao


-(Notice *)getNoticeWithID:(NSInteger)ID{
    Notice *notice = nil;
    FMResultSet *rs =[db executeQuery:[self SQL:@"select  * from %@ where id = ?" inTable:Notice_TABLE],
                      @(ID)];
    while ([rs next]) {
        notice = [self analysisNoticeWithRS:rs];
        
    }
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
	}
    return notice;
}

-(BOOL)isHasNotice:(Notice *)notice{
    BOOL success = NO;
    FMResultSet *rs =[db executeQuery:[self SQL:@"select * from %@  where id = ?" inTable:Notice_TABLE],@(notice.ID)];
    while ([rs next]) {
        success = YES;
        break;
    }
    return success;
}

-(BOOL)insertNotice:(Notice *)notice{
    BOOL success = YES;
	[db executeUpdate:[self SQL:@"INSERT INTO %@ (id,title,time,category_id,content) VALUES(?,?,?,?,?)" inTable:Notice_TABLE],
     [NSNumber numberWithInteger:notice.ID],
     notice.title,
     @([notice.time timeIntervalSince1970]),
     @(notice.categoryID),
     notice.content
     ];
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
		success = NO;
    }
	return success;
}
-(BOOL)saveWithNotices:(NSArray *)notices{
    BOOL success = YES;
    for (Notice *notice in notices) {
        BOOL isHas = [self isHasNotice:notice];
        isHas ?([self updateNotice:notice]):([self insertNotice:notice]);
    }
	return success;
}
-(BOOL)updateNoticeContent:(Notice *)notice{
    BOOL success = YES;
    [db executeUpdate:[self SQL:@"UPDATE %@ SET content = ? WHERE id = ?" inTable:Notice_TABLE],
     notice.content,
     @(notice.ID)
     ];
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
		success = NO;
    }
	return success;
}
-(BOOL)updateNotice:(Notice *)notice{
    BOOL success = YES;
    [db executeUpdate:[self SQL:@"UPDATE %@ SET category_id = ?,time = ? title = ? WHERE id = ?" inTable:Notice_TABLE],
     @(notice.categoryID),
      @([notice.time timeIntervalSince1970]),
     notice.title,
     @(notice.ID)
     ];
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
		success = NO;
    }
	return success;
}

-(NSArray *)getLatestNotices{
    NSMutableArray *notices = nil;
    FMResultSet *rs =[db executeQuery:[self SQL:@"select top 10 * from %@ order by time desc" inTable:Notice_TABLE]];
    while ([rs next]) {
        if (!notices) {
            notices = [NSMutableArray arrayWithCapacity:10];
        }
        Notice *notice = [self analysisNoticeWithRS:rs];
        [notices addObject:notice];
    }
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
	}
    return notices;
}
-(NSArray *)getNoticesWithCategory:(NSInteger)categoryID{
    NSMutableArray *notices = nil;
    FMResultSet *rs =[db executeQuery:[self SQL:@"select * from %@ where category_id = ? order by time desc" inTable:Notice_TABLE],@(categoryID)];
    while ([rs next]) {
        if (!notices) {
            notices = [NSMutableArray arrayWithCapacity:10];
        }
        Notice *notice = [self analysisNoticeWithRS:rs];
        [notices addObject:notice];
    }
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
	}
    return notices;
}
-(BOOL)clearNotice{
    BOOL success = YES;
	[db executeUpdate:[self SQL:@"delete from %@" inTable:Notice_TABLE]];
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
        success = NO;
	}
    return success;
}

-(Notice *)analysisNoticeWithRS:(FMResultSet *)rs{
    Notice *notice = [[Notice alloc]init];
    notice.ID = [rs intForColumn:@"id"];
    notice.title = [rs stringForColumn:@"title"];
    notice.content =[rs stringForColumn:@"content"];
    notice.time =[NSDate dateWithTimeIntervalSince1970:[rs doubleForColumn:@"time"]];
    notice.categoryID =[rs intForColumn:@"category_id"];
    return notice;
}

@end
